package top.continew.admin.common.util;

import java.security.MessageDigest;

/**
 * @ClassName ShaUtil
 * @Description sha工具
 * @Author kellen
 * @Date 2025/7/24 21:22
 */

public class ShaUtil {

    /**
     * SHA1加密方法
     */
    public static String sha1(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            byte[] digest = md.digest(str.getBytes("UTF-8"));
            StringBuilder hexStr = new StringBuilder();

            // 字节数组转十六进制
            for (byte b : digest) {
                String shaHex = Integer.toHexString(b & 0xFF);
                if (shaHex.length() < 2) {
                    hexStr.append(0);
                }
                hexStr.append(shaHex);
            }
            return hexStr.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
